package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import service.examinfo.ExaminfoService;
import service.majorunit.MajorunitService;
import service.position.PositionService;
import service.review.ReviewService;
import service.truth.TruthService;
import util.returnvo.ExaminfoListReturnVO;
import util.returnvo.MajorunitReturnVO;
import util.returnvo.ReviewListReturnVO;
import vo.ExaminfoVO;
import vo.MajorunitVO;
import vo.MemberVO;
import vo.PositionVO;
import vo.ReviewVO;

public class ReviewContoller extends MultiActionController{
	
	private ReviewService reviewService;
	private MajorunitService majorunitService;
	private TruthService truthService;
	private ExaminfoService examinfoService;
	private PositionService positionService;

	public void setReviewService(ReviewService reviewService) {
		this.reviewService = reviewService;
	}
	
	public void setMajorunitService(MajorunitService majorunitService) {
		this.majorunitService = majorunitService;
	}

	public void setTruthService(TruthService truthService) {
		this.truthService = truthService;
	}
	
	public void setExaminfoService(ExaminfoService examinfoService) {
		this.examinfoService = examinfoService;
	}
	
	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}
	
	/**
	 * 작성버튼 두개 나오는 페이지로 이동
	 */
	public ModelAndView write(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("review/write");
	}

	/**
	 * 리뷰페이지로. 
	 */
	public ModelAndView review(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		String page=request.getParameter("page");
		
		ReviewListReturnVO rList = reviewService.getReviewList(majorunit_no, page);
		
		MajorunitReturnVO mrvo=majorunitService.getOneUnitReturn(majorunit_no);
		
		ModelAndView mv=new ModelAndView("review/review");
		
		mv.addObject("rList", rList);
		
		mv.addObject("mrvo", mrvo);
		
		return mv;
	}
	
	/**
	 * 입시정보페이지로.
	 */
	public ModelAndView exam(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		String page=request.getParameter("page");
		
		ExaminfoListReturnVO eList=examinfoService.getExaminfos(majorunit_no, page);
		
		MajorunitReturnVO mrvo=majorunitService.getOneUnitReturn(majorunit_no);
		
		ModelAndView mv=new ModelAndView("review/exam");
		
		mv.addObject("eList", eList);
		
		mv.addObject("mrvo", mrvo);
		
		return mv;
	}
	
	/**
	 * ajax로. 숫자는 페이지 상에서 1추가 시키면 딱히 뭔가 반환하지 않아도 될 듯
	 */
	public ModelAndView voteTruth(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int review_no=Integer.parseInt(request.getParameter("review_no"));
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		int truth_vote=Integer.parseInt(request.getParameter("truth_vote"));
		
		int message=truthService.voteTruth(review_no, member_no, truth_vote);
		
		return new ModelAndView("JsonView", "message", message);
	}
	
	/**
	 * 세션의 member_no, majorunit_no로 소속 학과유닛을 가져온 후 majorunit_no가 -1이면 이미 작성하셨습니다 페이지로. 0보다 크면 리뷰작성 페이지로
	 */
	public ModelAndView writeReview(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		String position_no=request.getParameter("position_no");
		
		String path="review/did_write";
		
		ModelAndView mv=new ModelAndView(path);
		
		ReviewVO rvo=new ReviewVO(new MemberVO(member_no), new MajorunitVO(majorunit_no));
		
		if(reviewService.didWriteReview(rvo) < 1) {
			mv.setViewName("review/write_review");
			
			mv.addObject("majorunit_no", majorunit_no);
			mv.addObject("position_no", position_no);
		}
		
		return mv;
	}
	
	/**
	 * 입시리뷰 작성 전 학과 선택 페이지로.
	 */
	public ModelAndView selectUnitExaminfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("review/select_unit_exam");
	}
	
	/**
	 * 학과리뷰 작성 전 학과 선택 페이지로.
	 */
	public ModelAndView selectUnitReview(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<PositionVO> pList=positionService.getAllPosition();
		
		return new ModelAndView("review/select_unit_review", "pList", pList);
	}
	
	/**
	 * 리뷰등록
	 */
	public ModelAndView registerReview(HttpServletRequest request, HttpServletResponse response, ReviewVO review) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		
		int majorunit_no=review.getMajorunit().getMajorunit_no();
		
		review.setMember(new MemberVO(member_no));
		
		reviewService.registerReview(review);
		
		ModelAndView mv=new ModelAndView("redirect:review.do?command=review&&majorunit_no="+majorunit_no);
		
		return mv;
	}
	
	/**
	 * 리뷰수정
	 */
	public ModelAndView updateReview(HttpServletRequest request, HttpServletResponse response, ReviewVO review) throws Exception{
		review.setMember(new MemberVO((Integer)request.getSession().getAttribute("member_no")));
		reviewService.updateReview(review);
		return new ModelAndView("redirect:member.do?command=myReview");
	}
	
	/**
	 * 리뷰삭제
	 */
	public ModelAndView deleteReview(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int review_no=Integer.parseInt(request.getParameter("review_no"));
		System.out.println(review_no);
		reviewService.deleteReview(review_no);
		return null;
	}
	
	/**
	 * ExamInfo 작성시, 세션의 member_no와 파라미터의 majorunit_no으로 작성한 적 있는 학과인가 검색. 1이 넘어오면 이미 작성하셨습니다 페이지로.
	 */
	public ModelAndView writeExaminfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		String position_no=request.getParameter("position_no");
		
		String path="review/did_write";
		
		ModelAndView mv=new ModelAndView(path);
		
		ExaminfoVO evo=new ExaminfoVO(new MemberVO(member_no), new MajorunitVO(majorunit_no));
		
		if(examinfoService.didWriteExaminfo(evo) < 1) {
			mv.setViewName("review/write_exam");
			
			mv.addObject("majorunit_no", majorunit_no);
			mv.addObject("position_no", position_no);
		}
		
		return mv;
	}
	
	/**
	 * 입시정보등록
	 */
	public ModelAndView registerExaminfo(HttpServletRequest request, HttpServletResponse response, ExaminfoVO examinfo) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		
		int majorunit_no=examinfo.getMajorunit().getMajorunit_no();
		
		examinfo.setMember(new MemberVO(member_no));
		
		examinfoService.registerExaminfo(examinfo);
		
		ModelAndView mv=new ModelAndView("redirect:review.do?command=exam&&majorunit_no="+majorunit_no);
		
		return mv;
	}
	
	/**
	 * 입시정보수정
	 */
	public ModelAndView updateExaminfo(HttpServletRequest request, HttpServletResponse response, ExaminfoVO examinfo) throws Exception{
		examinfo.setMember(new MemberVO((Integer)request.getSession().getAttribute("member_no")));
		examinfoService.updateExaminfo(examinfo);
		return new ModelAndView("redirect:member.do?command=myExam");
	}
	
	/**
	 * 입시정보삭제
	 */
	public ModelAndView deleteExaminfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int examinfo_no=Integer.parseInt(request.getParameter("examinfo_no"));
		examinfoService.deleteExaminfo(examinfo_no);
		return null;
	}
	
	/**
	 * 신고하기
	 */
	public ModelAndView accuse(HttpServletRequest request, HttpServletResponse response, ExaminfoVO examinfo) throws Exception{
		//int member_no=(int)session.getAttribute("member_no");
		
		System.out.println("accuse called");
		
		return null;
	}
}
